﻿Imports System.ComponentModel
''' <summary>
''' Customize version of DevExpress SearckLookupEdit to support MetaQuery Definition
''' </summary>
''' <remarks></remarks>
Public Class MetaSearchLookUpEdit
    Inherits SearchLookUpEdit

    ''' <summary>
    ''' Query name use to fill the DataSource
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    <Description("QueryName use to Fill the DataSource")>
    Public Property QueryName As String

    ''' <summary>
    ''' Use to filter the query
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Property SQLWhere As String

    ''' <summary>
    ''' MetaQuery use to Fill the DataSource
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Protected Property Mq As MetaQuery

    Public Sub New()
        MyBase.New()
    End Sub

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="queryName">Query name use to fill the DataSource</param>
    ''' <remarks>QueryName must be different than Empty or Nothing</remarks>
    Public Sub New(ByVal queryName As String)
        MyBase.New()
        If Not String.IsNullOrWhiteSpace(queryName) Then
            Me.QueryName = queryName
        End If
        InitMetaQuery()
    End Sub

    ''' <summary>
    ''' Form Display from the ComboBox button
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Protected Overrides Function CreatePopupForm() As DevExpress.XtraEditors.Popup.PopupBaseForm
        Return New MetaPopUpSearchLookupEdit(Me)
    End Function

    ''' <summary>
    ''' Validate and Fill the Data from the QueryName
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub InitMetaQuery()
        Mq = MetaQuery.GetMetaQueryByName(QueryName)
        If Mq IsNot Nothing Then
            If Not String.IsNullOrWhiteSpace(Me.SQLWhere) Then
                Mq.SQLWhereContext = Me.SQLWhere
            End If
            Mq.FillResultQuery()
            Me.Properties.DataSource = Mq.ResultQuery
            InitGridView()
            Me.Refresh()
        Else
            MessageWindow.ShowMessage(New Exception("MetaSearchLookUpEdit MetaQuery not Found: " + QueryName))
        End If
    End Sub

    ''' <summary>
    ''' Customize the Grid according to the MetaCommand definition
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub InitGridView()
        Dim colVisible As Boolean
        Dim Column As DevExpress.XtraGrid.Columns.GridColumn
        Dim formatType As DevExpress.Utils.FormatType

        Dim hideCol As Boolean
        Dim colname As String

        'Field display in the ComboBox
        Me.Properties.DisplayMember = Mq.ResultQuery.Columns(CommonExt.Base.AppManager.ApplicationOption.DescriptionColumnIndex).ToString
        'Field use for the Value
        Me.Properties.ValueMember = Mq.GUIDField
        Me.Properties.NullText = "(None)"
        Me.Properties.NullValuePrompt = "(None)"
        Me.Properties.NullValuePromptShowForEmptyValue = True

        Me.Properties.View.OptionsView.ShowAutoFilterRow = True
        Me.Properties.View.HorzScrollVisibility = Views.Base.ScrollVisibility.Auto
        Me.Properties.View.VertScrollVisibility = Views.Base.ScrollVisibility.Auto
        Me.Properties.View.OptionsView.ColumnAutoWidth = False
        Me.Properties.View.Columns.Clear()
        For Each row As DataRow In Mq.MetaColumns.Rows
            colname = row("ColumnName").ToString
            hideCol = False
            If AppManager.ApplicationOption.DescriptionColumnIndex = 2 Then
                If colname.EndsWith("_ALT") Then
                    hideCol = True
                End If
            Else
                If colname.EndsWith("_PRM") Then
                    hideCol = True
                End If
            End If
            If hideCol = False Then
                colVisible = CBool(IIf(CInt(row("pVisible")) = 0, False, True))
                Column = Me.Properties.View.Columns.AddField(colname)
                Column.VisibleIndex = CInt(row("pOrder"))
                Column.Caption = row("pCaption").ToString
                Column.Visible = colVisible
                Column.Width = CInt(row("pWidth"))
                Column.ToolTip = row("pToolTip").ToString

                formatType = DevExGridHelper.GetFormatType(row)
                '
                Column.DisplayFormat.FormatType = formatType
                Column.DisplayFormat.FormatString = row("pNumberFormat").ToString
            End If
        Next
    End Sub
End Class
